﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerBase.Service.BaseDispatch;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using ServerBase.Service;
using ServerBase.Config;
using System;
using ServerBase.Other;
using System.Linq;
using ServerPublic.Manager;
using ServerPublic.Data;
using ServerGame.GameObject;
using static ServerGame.Service.Dispatcher;
using System.Diagnostics;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;

namespace ServerGame.Manager
{
    public partial class DispatcherMsg
    {
        [ApiDpPro((short)EProtocolId.C2G_MAIL_LIST, "邮件列表")]
        public short OnMailList()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnMailList();

            player.MailList(ref Rsp.ListMail);

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_MAIL_READ, "邮件读取")]
        public short OnMailRead()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnMailRead();
            Rsp.Id = Req.Id;

            if (!player.DaMail.SortDictMails.ContainsKey(Req.Id))
            {
                Rsp.Result = EProtocolResult.没有这封邮件; return Send(Rsp);
            }
            Rsp.Details = player.MailRead(Req.Id);

            player.InfoAll();
            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_MAIL_GET, "邮件领取附件")]
        public short OnMailGet()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnMailGet();
            Rsp.Id = Req.Id;

            if (!player.DaMail.SortDictMails.TryGetValue(Req.Id, out var mail))
            {
                Rsp.Result = EProtocolResult.没有这封邮件; return Send(Rsp);
            }
            if (!mail.HasAttachments)
            {
                Rsp.Result = EProtocolResult.没有附件; return Send(Rsp);
            }
            if (mail.Got)
            {
                Rsp.Result = EProtocolResult.附件已领取过; return Send(Rsp);
            }

            mail.MailGetTry(ref Rsp.listAward);
            mail.MailGet();
            AwardMgr.AwardSend(player, Rsp.listAward, mail.ActionSource);

            player.InfoAll();
            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_MAIL_GETALL, "邮件一键领取")]
        public short OnMailGetAll()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnMailGetAll();

            var listmailuids = new List<long>(player.DaMail.SortDictMails.Keys);
            foreach (var mailuid in listmailuids)
            {
                if (player.DaMail.SortDictMails.TryGetValue(mailuid, out var mail))
                {
                    List<CLS_AwardItem> listAward = new List<CLS_AwardItem>();
                    mail.MailGetTry(ref listAward);
                    AwardMgr.AwardSend(player, listAward, mail.ActionSource);
                    mail.MailGet();
                    Rsp.listAward.AddRange(listAward);
                }
            }
            Rsp.listAward = Rsp.listAward.Combine();

            player.InfoAll();

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_MAIL_DELETE, "邮件删除")]
        public short OnMailDelete()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnMailDelete();
            Rsp.Id = Req.Id;

            if (!player.MailDelete(Req.Id))
            {
                Rsp.Result = EProtocolResult.没有这封邮件;
            }

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_MAIL_DELETEREADED, "邮件清理已读")]
        public short OnMailDeleteReaded()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnMailDeleteReaded();

            player.MailDeleteReaded(Req.MailType);

            player.MailList(ref Rsp.ListMail);

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_MAIL_SAVE, "邮件保存/取消保存")]
        public short OnMailSave()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnMailSave();
            Rsp.Id = Req.Id;

            if (!player.DaMail.SortDictMails.ContainsKey(Req.Id))
            {
                Rsp.Result = EProtocolResult.没有这封邮件; return Send(Rsp);
            }
            player.MailSave(Req.Id);

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_MAIL_SEND, "邮件发送")]
        public short OnMailSend()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnMailSend();

            if (Req.MailInfoSend.MailType == EMailType.普通邮件)
            {
                if (player.Name.Equals(Req.MailInfoSend.ReceiveName))
                {
                    Rsp.Result = EProtocolResult.不能给自己发邮件; return Send(Rsp);
                }
                //var puid = BasePlayerManager.GetPuid(Req.MailInfoSend.ReceiveName);
                var puid = 0;//临时设置
                if (puid == 0)
                {
                    Rsp.Result = EProtocolResult.玩家不存在; return Send(Rsp);
                }
            }
            else
            {
                Rsp.Result = EProtocolResult.错误的邮件类型; return Send(Rsp);
            }

            //自己保存数据
            var sendmail = Req.MailInfoSend.MailCreate();
            sendmail.SenderName = player.Name;
            sendmail.Sended = true;
            sendmail.Readed = true;
            sendmail.Got = true;

            if (Req.MailInfoSend.MailType == EMailType.普通邮件)
            {
                sendmail.ReceiveName = Req.MailInfoSend.ReceiveName;

                var puid = PubPlayerMgr.GetPuid(Req.MailInfoSend.ReceiveName);
                var mail = Req.MailInfoSend.MailCreate();
                MailMgr.MailToPlayer(puid, mail);
            }

            Rsp.MailInfo = sendmail.MailInfo();
            //返回成功
            Send(Rsp);

            //保存已发
            MailMgr.MailToPlayer(player, sendmail);

            (ERecordType.发送邮件, EActionSource.系统).Rec(player.Puid, ((EMailType)Req.MailInfoSend.MailType).ToString(), Req.MailInfoSend.ReceiveName, Req.MailInfoSend.Body);

            return 0;
        }
    }
}